/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */
package com.sun.lwuit.resources.editor;

import java.io.IOException;
import java.util.prefs.Preferences;
import javax.swing.SwingUtilities;

/**
 * Shows a help file and allows the user to select never showing it again as an
 * option.
 *
 * @author Shai Almog
 */
public class ShowHelpDontAnnoy extends javax.swing.JDialog {
    private String resource;

    /** Creates new form ShowHelpOnce */
    private ShowHelpDontAnnoy(java.awt.Component parent, String resource) {
        super((java.awt.Frame)SwingUtilities.windowForComponent(parent), true);
        this.resource = resource;
        initComponents();
        try {
            help.setPage(getClass().getResource(resource));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        pack();
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    public static void showHelp(java.awt.Component parent, String resource) {
        if(!Preferences.userNodeForPackage(ShowHelpDontAnnoy.class).getBoolean(resource, false)) {
            new ShowHelpDontAnnoy(parent, resource);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dontShow = new javax.swing.JCheckBox();
        okButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        help = new javax.swing.JTextPane();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        dontShow.setText("Don't Show This Again");
        dontShow.setName("dontShow"); // NOI18N
        dontShow.addActionListener(formListener);

        okButton.setText("OK");
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(formListener);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        help.setContentType("text/html");
        help.setEditable(false);
        help.setName("help"); // NOI18N
        jScrollPane1.setViewportView(help);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(dontShow)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 202, Short.MAX_VALUE)
                        .add(okButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(dontShow)
                    .add(okButton))
                .addContainerGap())
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == dontShow) {
                ShowHelpDontAnnoy.this.dontShowActionPerformed(evt);
            }
            else if (evt.getSource() == okButton) {
                ShowHelpDontAnnoy.this.okButtonActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    public static void reset(String resource) {
            Preferences.userNodeForPackage(ShowHelpDontAnnoy.class).remove(resource);
    }

    private void dontShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dontShowActionPerformed
        Preferences.userNodeForPackage(ShowHelpDontAnnoy.class).putBoolean(resource, dontShow.isSelected());
    }//GEN-LAST:event_dontShowActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox dontShow;
    private javax.swing.JTextPane help;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

}
